
#include "head.h"

// 测试老师使用的模块 使用  消息队列  发送自己写的死消息  规定好的消息

typedef struct
{
    float temp;
    unsigned char hume;
    unsigned short iull;
    char led_state;
    char beep_state;
    char fan_state;
} Env_t;

typedef struct
{
    float temp_up;
    float temp_down;
    unsigned char hume_up;
    unsigned char hume_down;
    unsigned short iull_up;
    unsigned short iull_down;
} Limit_t;

typedef struct
{
    char led;
    char beep;
    char fan;
} Detctrl_t;

typedef struct msgbuf
{
    long mtype; /* message type, must be > 0 */
    /*环境数据结构*/
    Env_t env;
    /*阈值设置结构*/
    Limit_t limit;
    /*设备控制结构*/
    Detctrl_t dev;

} Msg_t;

#define MSGSIZE (sizeof(Msg_t) - sizeof(long))

int main()
{

    key_t key;
    int msqid;
    // 1.获取键值
    if ((key = ftok("/home", 'a')) == -1)
        PRINT_ERR("ftok error");
    // 2.创建消息队列
    if ((msqid = msgget(key, IPC_CREAT | 0666)) == -1)
        PRINT_ERR("msgget error");
    printf("    %s 创建的消息队列信息 【%p】 ", __FILE__, key);

    // 3.从消息队列中接消息
    int ret;
    long type;
    Limit_t limit = {.hume_down = 39,
                     .hume_up = 24,
                     .iull_down = 23,
                     .iull_up = 25,
                     .temp_down = 33,
                     .temp_up = 43};
    Detctrl_t dev;
    Env_t env;

    Msg_t msg = {.dev = dev, .env = env, .limit = limit};

    printf("%s %s %d \n", __FILE__, __func__, __LINE__);

    // type = 2;
    msg.mtype = 2;
    // strcpy(msg.mtype, "信息具体内容\n");
    // msgsnd(msqid, &msg, MSGSIZE, 0);

    printf("---拿到的具体数据为hun_down【%d】【%d】iull_down【%d】【%d】temp_down【%f】【%f】",
           msg.limit.hume_down,
           msg.limit.hume_up,
           msg.limit.iull_down,
           msg.limit.iull_up,
           msg.limit.temp_down,
           msg.limit.temp_up);

    printf("%s %s %d \n", __FILE__, __func__, __LINE__);

    msgsnd(msqid, &msg, MSGSIZE, 0);

    msgrcv(msqid, &msg, sizeof(Msg_t) - sizeof(long), 0, 0);

    printf("接收到 回执消息  证明 这个操作  设置阈值成功了 test 接受的消息类型为   msg.type【%d】\n", msg.mtype);
    printf("---拿到的具体数据为hun_down【%d】【%d】iull_down【%d】【%d】temp_down【%f】【%f】",
           msg.limit.hume_down,
           msg.limit.hume_up,
           msg.limit.iull_down,
           msg.limit.iull_up,
           msg.limit.temp_down,
           msg.limit.temp_up);

    // printf("其他属性 【%s】 \n",msg.dev.led);
}